CXX = $(TOOLCHAIN)g++
AR = $(TOOLCHAIN)ar
RANLIB = $(TOOLCHAIN)ranlib
NM = $(TOOLCHAIN)nm
MAKE = make
CFLAGS = -g -fPIC
CXXFLAGS = 
LDFLAGS = 
ifeq ($(STATIC), 1)
LDFLAGS += -static
endif
BINDIR = ../bin
OBJDIR = ../obj

LIB_ROOT = .
INCLUDE = $(LIB_ROOT)/include
LIB = $(LIB_ROOT)/lib

CXXFLAGS += -I$(INCLUDE)
#LDFLAGS +=  -L$(LIB) -ldl -lncurses $(LIB)/libreadline.a
LDFLAGS +=  -ldl -lncurses -lreadline -lpthread
LIBLDFLAGS = -fPIC -shared /usr/local/lib/libprotobuf.a -lpthread
PROTOC = protoc


SOCKTEST = $(BINDIR)/socktest
SOCKTESTLIB = $(BINDIR)/libsocktest.a
BIN = $(SOCKTEST) $(SOCKTESTLIB) $(patsubst %.a, %.so, $(SOCKTESTLIB))

OBJ = $(patsubst %.cpp, $(OBJDIR)/%.o, $(wildcard *.cpp))
COMMOBJ = $(OBJDIR)/socktest.o $(OBJDIR)/Socket.o $(OBJDIR)/Section.o $(OBJDIR)/CmdParser.o $(OBJDIR)/CommCmd.o
TESTOBJ = 
COMMOBJ += $(TESTOBJ)
PB = $(wildcard *.pb.*)

	
.PHONY: all clean mkdir install
all: $(BIN)
	$(MAKE) -C ../plugin/file/src

clean:
	$(MAKE) -C ../plugin/file/src clean
	-rm -rf $(OBJ) $(BIN) $(PB) $(BINDIR)/socktest.tar.bz2
	
mkdir:
	-ln -s /usr/local/lib/libreadline.a $(LIB)/libreadline.a
	-mkdir -p $(BINDIR)

PREFIX = ./release

pack: all
	-mkdir -p $(PREFIX)/include/socktest
	-mkdir -p $(PREFIX)/lib/socktest
	-mkdir -p $(PREFIX)/bin
	-cp $(SOCKTEST) $(BINDIR)/sendf.st $(BINDIR)/recvf.st $(PREFIX)/bin/
	-cp $(BINDIR)/*.slb $(PREFIX)/lib/socktest/
	-cp $(SOCKTESTLIB) $(PREFIX)/lib/
	-cp $(patsubst %.a, %.so, $(SOCKTESTLIB)) $(PREFIX)/lib/
	-cp socktest.h $(PREFIX)/include/socktest/
	-cp Socket.h $(PREFIX)/include/socktest/
	-cp Section.h $(PREFIX)/include/socktest/
	-cp CmdParser.h $(PREFIX)/include/socktest/
	-cd $(PREFIX) && tar jcf socktest.tar.bz2 bin include lib

$(SOCKTEST): $(COMMOBJ)
	$(CXX) $^ -o $@ $(CFLAGS) $(LDFLAGS)
	
$(SOCKTESTLIB): $(OBJDIR)/socktest.o $(OBJDIR)/Socket.o $(OBJDIR)/Section.o $(OBJDIR)/CmdParser.o
	$(AR) -cr $@ $^
	$(RANLIB) $@
#	$(NM) $@
	$(CXX) $^ -o $(patsubst %.a, %.so, $@) $(CFLAGS) $(LDFLAGS) $(LIBLDFLAGS)
	
sample.cpp: MsgBusBody.pb.cpp
	
$(PLUGINLIB): $(OBJDIR)/sample.o $(OBJDIR)/MsgBusBody.pb.o $(SOCKTESTLIB)
	$(CXX) $^ -o $@ $(CFLAGS) $(LDFLAGS) $(LIBLDFLAGS)
#	$(NM) $@
	

%.pb.cpp: %.proto
	$(PROTOC) $< --cpp_out=./
	mv $(@:.pb.cpp=.pb.cc) $@


$(OBJDIR)/%.o: %.cpp
	$(CXX) $< -c -o $@ $(CFLAGS) $(CXXFLAGS)
	
	
